# !/usr/bin/python
# -*- coding: utf-8 -*-

# mediaplex
# multimedia platform
#
# this file is licensed under the MIT license
# read "LICENSE.MIT" in the root of this distribution

__authors__ = 'Jan Jokela <janjokela@gmail.com>'
__description__ = 'Configuration backends'

import mediaplex.core.log as log

class Backend:
    def __init__(self):
        """ This is nothing more than an interface for the
        inheriting classes to follow
        """
        pass
    def get_option(self, section, option, type='str'):
        """ get an option """
        pass
    def set_option(self, section, option, value):
        """ set an option """
        pass
    
class ConfParser(Backend):
    def __init__(self):
        import ConfigParser
        self._rebuild()
    def get_option(self, section, option, type='str'):
        for s in self.conf.sections():  
            if s == section:           
                for o in self.conf.options(section):
                    if o == option: 
                        # ConfigParser supports acquiring values in raw (str),
                        # int, float and boolean types
                        if type == 'str': return self.conf.get(section, key)
                        elif type == 'int': return self.conf.getint(section, key)
                        elif type == 'float': return self.conf.getfloat(section, key)
                        elif type == 'boolean': return self.conf.getboolean(section, key)
                        else:
                            log.debug('core', 'invalid return type in get_option')
    def set_option(self, section, option, value):
        self.conf.set(section, option, value)
    def _rebuild(self):
        conf_file = os.path.join(os.path.expanduser('~'), '.mediaplex', 'mediaplex.conf')
        self.conf = ConfigParser.ConfigParser()
        self.conf.read(file)
    def _verify_integrity(self):
        conf_file = os.path.join(os.path.expanduser('~'), '.mediaplex', 'mediaplex.conf')
        if not os.path.exists(conf_file):
             pass
    
class GConf(Backend):
    """ Not implemented yet """
    log_msg = 'GConf configuration backend not implemented yet'
    def __init__(self):
        log.error('core', log_msg)
    def get_option(self, component, section, option):
        log.error('core', log_msg)
    def set_option(self, component, section, option, value):
        log.error('core', log_msg)
